/*
 * @FileName: app_fault.c
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-07-18 10:14:16
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-09-18 08:57:35
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */

#include "bsp.h"
#include "fault.h"
#include "app_fault.h"
#include "app_analog.h"

uint16_t g_fault_bits[BOOST_NUM]; // 16位数，每一位代表对应fault的状态

/**
 * @description: 故障表
 */
fault_t ft_tbl[BOOST_NUM][FAULT_NUM] =
{
 {
     {.id = FAULT_VIN_UVP,  .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_1][ANALOG_ID_VIN].real_flt),  .polarity = FAULT_CMP_POL_LOW,  .auto_clear = true, .threshold = 16.0f, .trig_time = 500, .recovery_time = 3000},
     {.id = FAULT_VOUT_OVP, .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_1][ANALOG_ID_VOUT].real_flt), .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 62.0f, .trig_time = 50, .recovery_time = 3000},
     {.id = FAULT_IOUT_OCP, .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_1][ANALOG_ID_IIN].real_flt),  .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 18.0f, .trig_time = 50, .recovery_time = 3000},
     {.id = FAULT_OTP1,     .flag = false, .singal_addr = &g_mos_ftemp,                                       .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 115.0f,.trig_time = 3000, .recovery_time = 300000},
     {.id = FAULT_OTP2,     .flag = false, .singal_addr = &g_mcu_ftemp,                                       .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 105.0f,.trig_time = 3000, .recovery_time = 300000},
     {.id = FAULT_VIN_OVP,  .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_1][ANALOG_ID_VIN].real_flt),  .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 60.5f, .trig_time = 500, .recovery_time = 3000},
 },


    {
        {.id = FAULT_VIN_UVP,  .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_2][ANALOG_ID_VIN].real_flt),  .polarity = FAULT_CMP_POL_LOW,  .auto_clear = true, .threshold = 16.0f, .trig_time = 500, .recovery_time = 3000},
        {.id = FAULT_VOUT_OVP, .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_2][ANALOG_ID_VOUT].real_flt), .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 62.0f, .trig_time = 50, .recovery_time = 3000},
        {.id = FAULT_IOUT_OCP, .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_2][ANALOG_ID_IIN].real_flt),  .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 18.0f, .trig_time = 50, .recovery_time = 3000},
        {.id = FAULT_OTP1,     .flag = false, .singal_addr = &g_mos_ftemp,                                       .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 115.0f,.trig_time = 3000, .recovery_time = 300000},
        {.id = FAULT_OTP2,     .flag = false, .singal_addr = &g_mcu_ftemp,                                       .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 105.0f,.trig_time = 3000, .recovery_time = 300000},
        {.id = FAULT_VIN_OVP,  .flag = false, .singal_addr = &(analog_tbl[BOOST_ID_2][ANALOG_ID_VIN].real_flt),  .polarity = FAULT_CMP_POL_HIGH, .auto_clear = true, .threshold = 60.5f, .trig_time = 500, .recovery_time = 3000},
    }
};

/**
 * @description: 故障模块主任务
 * @return 无
 */
void app_fault_run(void)
{
    for (uint16_t boostId = 0; boostId < BOOST_NUM; boostId++) {
        for (uint16_t id = 0; id < FAULT_NUM; id++) {
            fault_check(&ft_tbl[boostId][id]);

            if (ft_tbl[boostId][id].flag == true) {
                g_fault_bits[boostId] |= (1 << id); // 对应bit位置1
            } else {
                g_fault_bits[boostId] &= (~(1 << id)); // 对应bit位清0
            }
        }
    }
}

/**
 * @description: 检查故障被触发
 * @param id 故障id
 * @return 无
 */
uint16_t app_fault_get_bits(uint16_t boostId)
{
    return g_fault_bits[boostId];
}
